/*
 * Singularity Engine
 * Copyright (C) 2010 Vyacheslav Vorobyov, All rights reserved.
 * See LICENSE for full license information.
 */

package org.singularity.io;

import java.io.IOException;
import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author vjache
 *
 */
public class BroadcastPipe<T> implements CloseablePipe<T> {
	private static Log LOG = LogFactory.getFactory().getInstance(BroadcastPipe.class);

	private final Collection<CloseablePipe<T>> _pipes;

	/**
	 * 
	 */
	public BroadcastPipe(Collection<CloseablePipe<T>> aTargetPipes) {
		_pipes = aTargetPipes;
	}

	/* (non-Javadoc)
	 * @see java.io.Closeable#close()
	 */
	@Override
	public void close() throws IOException {
		for(CloseablePipe<T> p: _pipes)
		{
			try{p.close();}catch(IOException e)
			{
				LOG.error(String.format("Error while closing %s pipe.", p), e);
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.singularity.io.Pipe#flush()
	 */
	@Override
	public void flush() throws IOException {
		for(CloseablePipe<T> p: _pipes)
			p.flush();
	}

	/* (non-Javadoc)
	 * @see org.singularity.io.Pipe#push(java.lang.Object)
	 */
	@Override
	public void push(T aItem) throws IOException {
		for(CloseablePipe<T> p: _pipes)
			p.push(aItem);
	}

}
